package com.htpay.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htpay.aspect.AutoLog;
import com.htpay.model.common.ResponseBean;
import com.htpay.model.entity.MerchantChannel;
import com.htpay.model.entity.MerchantOrder;
import com.htpay.service.IMerchantChannelService;
import com.htpay.service.IMerchantOrderService;
import com.htpay.util.MerchantUtil;
import com.htpay.util.support.QueryGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sys/trade")
public class TradeController {

    @Autowired
    private IMerchantOrderService merchantOrderService;

    @Autowired
    private IMerchantChannelService merchantChannelService;

    @AutoLog(value = "交易订单表查询")
    @GetMapping(value = "/orderQuery")
    public ResponseBean queryPageList(MerchantOrder merchantOrder,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      HttpServletRequest req) {
        ResponseBean bean = new ResponseBean();
        Integer merchantId = MerchantUtil.getMerchantId(req);
        log.info("==== merchantId [" + merchantId + "]正在获取order query分页接口数据 ====");
        merchantOrder.setMerchantId(merchantId);

        QueryWrapper<MerchantOrder> queryWrapper = QueryGenerator.initQueryWrapper(merchantOrder, req.getParameterMap());
        Page<MerchantOrder> page = new Page<MerchantOrder>(pageNo, pageSize);

        IPage<MerchantOrder> pageList = merchantOrderService.page(page, queryWrapper);
        log.info("=============== 交易订单表查询 ================");
        log.info("查询当前页：" + pageList.getCurrent());
        log.info("查询当前页数量：" + pageList.getSize());
        log.info("查询结果数量：" + pageList.getRecords().size());
        log.info("数据总数：" + pageList.getTotal());
        log.info("================== END =============");
        return bean.success("操作成功!", pageList);
    }

    @AutoLog(value = "资费查询")
    @GetMapping(value = "/rateQuery")
    public ResponseBean queryRateList(HttpServletRequest req) {
        ResponseBean bean = new ResponseBean();
        Integer merchantId = MerchantUtil.getMerchantId(req);

        //查询费率
        List<MerchantChannel> mchannelList = merchantChannelService.queryChannelListByMerchantId(merchantId);
        return bean.success("操作成功！", mchannelList);
    }


}
